import router from './router'
import store from './store'
// import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login'] // no redirect whitelist
const blackList = ['/login'] // no redirect blacklist

router.beforeEach(async(to, from, next) => {
  NProgress.start() // 进度条开始
  document.title = getPageTitle(to.meta.title)
  const localToken = getToken()
  if (localToken) {
    if (blackList.includes(to.path)) {
      next('/workbench')
      NProgress.done()
    } else {
      if (!store.getters.userInfo.id) {
        store.dispatch('user/getUserInfoAction')
      }
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
      NProgress.done()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
